<template>
	<view class="content">
		<u-navbar :title="text" bgColor="#fafafa" safeAreaInsetTop :autoBack="true" :placeholder="true">
			<view class="u-nav-slot" slot="left">
				<text class="iconfont icon-fanhui"></text>
				<view class="fresh-nav" v-if="isShow">
				<view class="iconfont icon-address"><text>内江市</text></view>
				<text class="iconfont icon-jinrujiantouxiao"></text>
				</view>
				<view class="fresh-nav-seach" v-else :style="{marginRight:navWidth + 'px'}">
				<u-input placeholder="请输入商品名称" border="none"
				 v-model="value" shape="circle" prefixIcon="search"
				 :customStyle="{
					   backgroundColor:'#fff',
					   padding:'4rpx 0rpx 4rpx 40rpx'
				 }"
				 >
				 <template slot="suffix">
					<view class="fresh-sou">搜索</view>
				 </template>
				 </u-input>
				 </view>
			</view>
		</u-navbar>
		<view class="fresh-input">
			<view class="search">
			  <view class="search_icon">
			    <u-icon name="search" color="#000" size="18"></u-icon>
			  </view>
			  <input class="uni-input" disabled placeholder="请输入商品名称" @click="searchbtn" />
			  <button class="sous" @click="searchbtn">搜索</button>
			</view>
			   <view class="minititle">
				   <view class="miniitem">
					   <text class="iconfont icon-address add"></text>
					   <text>产地直采</text>
				   </view>
				   <view class="miniitem">
					   <text class="iconfont icon-hezi201 add"></text>
					   <text>足斤足量</text>
				   </view>
				   <view class="miniitem">
					   <text class="iconfont icon-snowflake add"></text>
					   <text>全程冷链</text>
				   </view>
				   <view class="miniitem">
					   <text>急速配送</text>
				   </view>
			   </view>
		</view>
		<view class="fersh_option" :style="{top:(top+18)+'rpx'}">
			<u-scroll-list indicator>
			<view class="optionitem" v-for="item in fresh_cate" :key="item.id" @click="storebtn(item.id)">
				<view class="img"><image :src="item.pic" mode="widthFix"></image></view>
				<view class="text">{{item.cate_name}}</view>
			</view>
			</u-scroll-list>
		</view>
		<view class="fresh_store">
			<view class="fresh_store_te">人气热卖</view>
			<view class="fresh_const">
				<FreshCommodity ref="delson" :freshlist="fresh_list"></FreshCommodity>
			</view>
			<u-empty v-if="fresh_list.length === 0" mode="list" text=' '></u-empty>
		</view>
		<u-loadmore :status="status"/>
	</view>
</template>

<script>
	import FreshCommodity from '../component/freshcommodity.vue'
	import {getproduct,getcalss} from '@/api/commodity.js'
	export default{
		data(){
			return{
				text:'迷彩生鲜',
				isShow:true,
				navWidth:null,
				top:0,
				formprod: {
				  page: 1,
				  limit: 10,
				  cate_id:46
				},
				status: 'loadmore',
				fresh_cate:[],
				fresh_list:[],
				count:0
			}
		},
		components:{
			FreshCommodity
		},
		methods:{
			async getfreshstoreapi(){
				const res = await getproduct(this.formprod)
				this.fresh_list = res.data.list
				this.count = res.data.count
				this.count > this.fresh_list.length ? this.status = 'loadmore' : this.status = 'nomore'
			},
			async assortapi(){
				const res = await getcalss(46)
				this.fresh_cate = res.data
			},
			storebtn(id){
				this.$refs.delson.remove()
				this.formprod.cate_id = id
				this.formprod.cate_type = 2
				this.getfreshstoreapi()
			}
		},
		onPageScroll(el) {
		  const windowHeight = uni.getSystemInfoSync().windowHeight;
		  if(el.scrollTop >= this.top/2-10){
			  this.text = ''
			  this.isShow = false
		  }else{
			  this.text = '迷彩生鲜'
			  this.isShow = true
		  }
		},
		mounted() {
			uni.createSelectorQuery().select('.fersh_option').boundingClientRect(data => {
			this.top = data.top; // 获取到顶部的距离
			}).exec();
		},
		onLoad() {
			let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			this.navWidth=menuButtonInfo.width
			this.getfreshstoreapi()
			this.assortapi()
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #fafafa;
	}
	.content{
		padding: 0 30rpx;
	}
	.u-nav-slot{
		display: flex;
		align-items: center;
		.icon-fanhui{
			font-size: 40rpx;
			font-weight: bold;
		}
		.fresh-nav{
			display: flex;
			align-items:center;
		.icon-address{
			font-size: 34rpx;
			text{
				font-size: 30rpx;
				margin-left: 10rpx;
			}
		}	
		}
		.fresh-nav-seach{
			.fresh-sou{
				background-color: #095907;
				color: white;
				padding:6rpx 20rpx;
				border-radius: 30rpx;
				font-size: 28rpx;
			}
		}
	}
	.fresh-input{
		.search {
		  position: relative;
		  display: flex;
		  align-items: center;
		  .search_icon {
		    position: absolute;
		    left: 20rpx;
		  }
		  .uni-input {
		    background-color: #fff;
		    height: 68rpx;
		    border-radius: 50rpx;
		    width: 100%;
		    font-size: 24rpx;
		    padding: 0rpx 50rpx;
		  }
		  .sous {
		    position: absolute;
		    border-radius: 50rpx;
		    right: 0;
		    height: 68rpx;
		    width: 140rpx;
		    line-height: 68rpx;
		    text-align: center;
		    font-size: 24rpx;
		    color: white;
		    background-color: #095907;
		  }
		}
		.minititle{
			font-size: 22rpx;
			margin-top: 20rpx;
			display: flex;
			justify-content: space-around;
			.miniitem{
				display: flex;
				align-items: flex-end;
				justify-content: center;
				.add{
					font-size: 26rpx;
					margin-right: 10rpx;
				}
			}
		}
	}
	.fersh_option{
		box-sizing: border-box;
		overflow: hidden;
		padding-top: 20rpx;
		position: sticky;
		background-color: #fafafa;
		border-radius: 10rpx;
		.optionitem{
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-right: 30rpx;
			font-size: 26rpx;
			text-align: center;
			.img{
				width: 100rpx;
				height: 100rpx;
				overflow: hidden;
				border-radius: 50%;
				margin-bottom: 10rpx;
				image{
					width: 100rpx;
				}
			}
			.text{
				width:120rpx ;
			}
		}
	}
	.fresh_store{
		margin-top: 20rpx;
		.fresh_store_te{
			margin-bottom: 30rpx;
			font-size: 30rpx;
			color:#06ba2e;
			font-weight: bold;
		}
		.fresh_const{
			display: flex;
			flex-wrap: wrap;
		}
	}
</style>